﻿

namespace Mulala.Cavan.Domain.MainBoundedContext.MulalaModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Core.Persistent;
    using Mulala.Cavan.Domain.MainBoundedContext.LogModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Core.MyLinq;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;

    public class VisitHistorySpecification : ISpecification<VisitHistory, VisitHistorySearch>
    {
        public VisitHistorySearch SearchParams
        {
            get;
            set;
        }

        public System.Linq.Expressions.Expression<Func<VisitHistory, bool>> Expression
        {
            get 
            {
                IQueryBuilder<VisitHistory> queryBuilder = QueryBuilder.Create<VisitHistory>();

                if (null != SearchParams)
                {
                    if (SearchParams.IntervieweeID > 0)
                    {
                        queryBuilder = queryBuilder.Equals(vh => vh.Interviewee.ID, SearchParams.IntervieweeID);
                    }
                    if (SearchParams.VisitorID > 0)
                    {
                        queryBuilder = queryBuilder.Equals(vh => vh.Visitor.ID, SearchParams.VisitorID);
                    }
                    if (SearchParams.LastVisitTime != DateTime.Parse(Project.Settings.DefaultTime))
                    {
                        queryBuilder = queryBuilder.Equals(vh => vh.LastVisitTime.Year, SearchParams.LastVisitTime.Year);
                        queryBuilder = queryBuilder.Equals(vh => vh.LastVisitTime.Month, SearchParams.LastVisitTime.Month);
                        queryBuilder = queryBuilder.Equals(vh => vh.LastVisitTime.Day, SearchParams.LastVisitTime.Day);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }
    }
}
